package com.magicsoft.app.helper;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


/**
 * @author zengweixin
 */
public class Formats {

    private static DecimalFormat doubleFormat = new DecimalFormat("#0.###");
    private static DecimalFormat moneyFormat = new DecimalFormat("###,###,###,##0.###");

    /**
     * format should not be changed if i start a export/import function *
     */
    private static DateFormat norDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
    private static DateFormat norDateFormatOld = new SimpleDateFormat("yyyy-MM-dd");
    private static DateFormat norDatetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
    private static DateFormat norDatetimeFormatOld = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static DecimalFormat norDoubleFormat = new DecimalFormat("#0.###");


    public static String double2String(Double d) {
        return double2String(d == null ? 0D : d);
    }

    public static String double2String(double d) {
        return doubleFormat.format(d);
    }

    public static double string2Double(String d) {
        try {
            return doubleFormat.parse(d).doubleValue();
        } catch (ParseException e) {
            Logger.error(e.getMessage());
            return 0D;
        }
    }

    public static String normalizeDouble2String(Double d) {
        return normalizeDouble2String(d == null ? 0D : d);
    }

    public static String normalizeDouble2String(double d) {
        return norDoubleFormat.format(d);
    }

    public static double normalizeString2Double(String d) throws ParseException {
        return norDoubleFormat.parse(d).doubleValue();

    }

    public static String normalizeDate2String(Date date) {
        return norDateFormat.format(date);
    }

    public static Date normalizeString2Date(String date) throws ParseException {
        try {
            return norDateFormat.parse(date);
        } catch (ParseException x) {
            return norDateFormatOld.parse(date);
        }
    }

    public static String normalizeDatetime2String(Date date) {
        return norDatetimeFormat.format(date);
    }

    public static Date normalizeString2Datetime(String date) throws ParseException {
        try {
            return norDatetimeFormat.parse(date);
        } catch (ParseException x) {
            return norDatetimeFormatOld.parse(date);
        }
    }

    public static String money2String(Double money, String symbol, SymbolPosition pos) {
        return money2String(money == null ? 0D : money.doubleValue(), symbol, pos);
    }

    public static String money2String(double money, String symbol, SymbolPosition pos) {
        StringBuilder sb = new StringBuilder();
        if (SymbolPosition.FRONT.equals(pos) && symbol != null) {
            sb.append(symbol);
        }
        sb.append(moneyFormat.format(money));
        if (SymbolPosition.AFTER.equals(pos) && symbol != null) {
            sb.append(symbol);
        }
        return sb.toString();
    }

    /*判断是否两位小数*/
    public static boolean check2DocInput(String s) {
        return s.matches("^(\\d*[.]?)(\\d{0,2})?$");
    }

    //手机号码格式
    public static String setPhoneNumberFormat(String s) {
        s = s.replaceAll("[\\s\\t\\r]", "");

        int len = s.length();
        int[] ints = new int[]{3, 8};//手入空格的位置
        for (int i : ints) {

            if (len > i) {
                s = s.substring(0, i) + " " + s.substring(i, len);
            }
            len = s.length();
        }

        return s;
    }
    
    /**
     * 判断是否为null或空值
     * 
     * @param str String
     * @return true or false
     */
    public static boolean isNullOrEmpty(String str)
    {
        return str == null || str.trim().length() == 0;
    }
    
    /**
	 * 手机号码 正则验证
	 */
	private static final String NUMBER_REGEX = "(\\+86|86|0086|17951|12593)?(13[0-9]|15[0-35-9]|14[57]|18[02356789])\\d{8}";

	public static String fixPortalPhoneNumber(String number) {
		if (isNullOrEmpty(number)) {
			return number;
		}

		number = number.replace("-", "").replace(" ", "");
		String retPhoneNumber = number.trim();

		// 确定是否是手机号码，然后将前缀去除，只保留纯号码
		if (isMobile(retPhoneNumber)) {
			if (retPhoneNumber.startsWith("+86")) {
				retPhoneNumber = retPhoneNumber.substring(3);
			} else if (retPhoneNumber.startsWith("86")) {
				retPhoneNumber = retPhoneNumber.substring(2);
			} else if (retPhoneNumber.startsWith("0086")) {
				retPhoneNumber = retPhoneNumber.substring(4);
			}else if (retPhoneNumber.startsWith("17951")) {
				retPhoneNumber = retPhoneNumber.substring(5);
			}else if (retPhoneNumber.startsWith("12593")) {
				retPhoneNumber = retPhoneNumber.substring(5);
			}
			
		}
		System.out.println(retPhoneNumber);
		return retPhoneNumber;
	}
    
	/**
	 * 验证字符串是否符合手机号格式
	 * 
	 * @param str
	 *            需要验证的字符串
	 * @return 不是手机号返回false，是手机号就返回true
	 */
	public static boolean isMobile(String str) {
		return str != null
				&& str.matches(NUMBER_REGEX);
	}
}
